package com.github.tetrisgame.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.github.tetrisgame.R;
import com.github.tetrisgame.databinding.ActivityLoginBinding;
import com.github.tetrisgame.utils.PermissionUtils;
import com.github.tetrisgame.utils.SkipToPhotoAlbum;
import com.github.tetrisgame.utils.StringUtils;
import com.github.tetrisgame.utils.TakePhotoUtils;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        mBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mBinding.etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() < 11) {
                    mBinding.btnVerify.setEnabled(false);
                }
                if (s.length() == 11) {
                    boolean isPhone = StringUtils.isPhone(s.toString());
                    if (isPhone) {
                        mBinding.btnVerify.setEnabled(true);
                    } else {
                        Toast.makeText(LoginActivity.this, "手机号不合法", Toast.LENGTH_SHORT).show();
                    }
                }
                if (s.length() > 11) {
                    s.delete(11, s.length());
                }
            }
        });
        mBinding.btnVerify.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, VerifyActivity.class);
            intent.putExtra("phone", mBinding.etPhone.getText().toString());
            startActivity(intent);
            //发送验证码
            BmobSMS.requestSMSCode(mBinding.etPhone.getText().toString().trim(), "DataSDK", new QueryListener<Integer>() {
                @Override
                public void done(Integer smsId, BmobException e) {
                    if (e == null) {
                        Intent intent = new Intent(LoginActivity.this, VerifyActivity.class);
                        intent.putExtra("phone", mBinding.etPhone.getText().toString());
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "发送验证码失败", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            });


        });
        PermissionUtils.sharedInstance.init(this, "同意权限后才可以设置头像");
        SkipToPhotoAlbum.sharedInstance.init(this);
        TakePhotoUtils.sharedInstance.init(this);
        //手动配置视图的属性
        //mBinding.headView.setBorderWidth(UIUtils.dp2px(10));
        //mBinding.headView.setBorderColor(Color.GREEN);
        //mBinding.headView.setShadowRadius(UIUtils.dp2px(12));
        //mBinding.headView.setShowPressEffect(true);

    }
}